﻿using ImageResizer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;


namespace Bids.Web.Controllers
{
    public class ImgController:Controller
    {
        public FileResult Get(string path,int? width,int? height)
        {
            var directory = path.Substring(0, path.LastIndexOf('/'));
            var filename = path.Substring(path.LastIndexOf('/') +1, path.Length - path.LastIndexOf('/')-1);
            var serverfile = Server.MapPath("/" + directory +"/" + filename);
            if (width.HasValue || height.HasValue)
            {
                if (!width.HasValue)
                    width = height.Value;
                if (!height.HasValue)
                    height = width.Value;
                filename = string.Format("{0}-{1}-{2}", width.Value, height.Value, filename);
                var serverfile2 = Server.MapPath("/" + directory + "/" + filename);
                if (!System.IO.File.Exists(serverfile2))
                {
                    new ImageJob(
                     serverfile,
                     serverfile2,
                     new ResizeSettings
                     {
                         Width = width.Value,
                         Height = height.Value
                     }
                     ).Build();
                    
                }
                return File(serverfile2, "image/jpg");
            }
            
            return File(Server.MapPath("/"+path), "image/jpg");
           
        }
    }
}
